﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core.Profile;
using Medianamik.Core.Security;
using Roles = Medianamik.Core.Security.Roles;
using Medianamik.Core;
using Medianamik.Globalization;

namespace Medianamik.UI.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GroupCheckBoxList runat=server></{0}:GroupCheckBoxList>")]
    public class GroupCheckBoxList : System.Web.UI.WebControls.CheckBoxList 
    {

        [Bindable(true)]
        public IEnumerable<string> SelectedGroups
        {
            get
            {
                return ViewState["SelectedGroups"] as IEnumerable<string>
                    ?? new string[] { };
            }

            set
            {
                ViewState["SelectedGroups"] = value;
                Select();
            }
        }

        public override string DataTextField
        {
            get
            {
                return "Text";
            }
            set
            {
                ;
            }
        }

        public override string DataValueField
        {
            get
            {
                return "Value";
            }
            set
            {
                ;
            }
        }

        public override object DataSource
        {
            get
            {
                return Roles.GetAllGroups()
                    .Where(g => g.ToLowerInvariant() != MedianamikGroups.LoweredSuperAdminGroupName )
                    .Select(g=> new 
                    {
                        Value = g,
                        Text = Translator.Translate("Medianamik_SecurityManagement", "Group_" + g) + " ("+g+")"                       
                    }).OrderBy(g=>g.Text);
            }
            set
            {
                throw new Exception("Cannot set DataSource of this control");
            }
        }

        public override void DataBind()
        {
            base.DataBind();
            Select();
        }

        private void Select()
        {
            foreach (ListItem item in Items)
            {
                item.Selected = SelectedGroups.Any(g => g.Equals(item.Value, StringComparison.OrdinalIgnoreCase));

                if(!MedianamikUser.IsSuperAdmin)
                    item.Enabled = MedianamikUser.Groups.GetAllAllowedGroups(MedianamikUserProfile)
                        .Any(g => g.Name.Equals(item.Value, StringComparison.OrdinalIgnoreCase));
            }
        }

        private MedianamikUser _medianamikUser;
        /// <summary>
        /// Usager "Médianamik" présentement connecté
        /// </summary>
        public MedianamikUser MedianamikUser
        {
            get
            {
                if (_medianamikUser == null)
                {
                    _medianamikUser = (MedianamikUser)Membership.GetUser();
                }
                return _medianamikUser;
            }
        }

        public UserProfile MedianamikUserProfile
        {
            get
            {
                return (UserProfile) HttpContext.Current.Profile;
            }

        }
    }
}
